﻿#include "injectortablemodel.h"

InjectorTableModel::InjectorTableModel()
{

}

InjectorTableModel::~InjectorTableModel()
{

}

Qt::ItemFlags InjectorTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;
    return QAbstractTableModel::flags(index);
}

QVariant InjectorTableModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
         const InjectorData& injector = m_list[index.row()];
         switch(index.column())
         {
            case 0 : return injector.status;
            case 1 : return injector.name;
            case 2 : return injector.battery;
            case 3 : return injector.oil;
            default: break;
         }
     }
     return QVariant();
}

QVariant InjectorTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if(orientation == Qt::Horizontal)
            return m_horizontalHeader.at(section);
        if(orientation == Qt::Vertical)
            return section;
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

int InjectorTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_list.size();
}

int InjectorTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_horizontalHeader.size();
}

bool InjectorTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(role);
    InjectorData& injector = m_list[index.row()];
    switch(index.column())
    {
       case 0 : injector.status = value.toInt(); return true;
       case 1 : injector.name = value.toString(); return true;
       case 2 : injector.battery = value.toInt(); return true;
       case 3 : injector.oil = value.toInt(); return true;
       default: return false;
    }
}

//resizable
bool InjectorTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row + count - 1);
    for(int i=row; i<row+count; i++)
        m_list.insert(i,InjectorData());
    endInsertRows();
    return true;
}

bool InjectorTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    for(int i=row+count-1;i>=row;i--)
    {
        m_list.removeAt(i);
    }   // for
    endRemoveRows();
    return true;
}

//own
void InjectorTableModel::setHorizontalHeader(const QStringList &headers)
{
    m_horizontalHeader =  headers;
}

const InjectorData& InjectorTableModel::getDeviceData(const QModelIndex &index) const
{
    return m_list[index.row()];
}

void InjectorTableModel::setList(const QList<InjectorData>& data)
{
    m_list = data;
}

bool InjectorTableModel::addDevice(const InjectorData &data)
{
    insertRow(m_list.size());
    m_list.back() = data;
    return true;
}

bool InjectorTableModel::deleteDevice(const QModelIndex &index)
{
    removeRow(index.row());
    return true;
}

uint InjectorTableModel::getDeviceId(const QModelIndex& index) const
{
    return m_list[index.row()].id;
}

void InjectorTableModel::setDeviceData(const QModelIndex& index, const InjectorData& data)
{
    beginResetModel();
    m_list[index.row()] = data;
    endResetModel();
}

void InjectorTableModel::updateStatus(const SerialPack& pack)
{
    auto it = m_list.begin();
    for(;it!=m_list.end();it++)
        if(it->ip == pack.ip)
            break;
    beginResetModel();
    it->status = pack.status;
    it->battery = pack.battery;
    it->oil = pack.oil;
    it->settings.inject_volume = pack.settings.inject_volume;
    it->settings.inject_period = pack.settings.inject_period;
    it->settings.comm_period = pack.settings.comm_period;
    //qDebug()<< pack.oil<<" "<<pack.settings.inject_volume<<" "<< pack.settings.inject_period<<endl;
    endResetModel();
}



